package com.ruoyi.groovy.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sign.Base64;
import com.ruoyi.groovy.domain.GroovyScriptModel;
import com.ruoyi.groovy.service.GroovyScriptService;
import com.ruoyi.groovy.service.LoadScriptSerivce;
import com.ruoyi.groovy.utils.GroovyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务聚合
 *
 * @version [V2.0]
 * @package com.ruoyi.groovy.controller;
 * @description:[用一句话描述该文件做什么]
 * @author: [zhuben]
 * @date: [2024-12-16 09:09]
 * @copyright: 2024 echovin.com
 */
@RestController
@RequestMapping("groovy")
public class GroovyController{
    private static final Logger log = LoggerFactory.getLogger(GroovyController.class);
    @Resource
    private GroovyScriptService groovyScriptService;
    @Resource
    private LoadScriptSerivce loadScriptSerivce;

    /**
     * 保存或修改
     * @param model
     * @return
     */
    @PostMapping("saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody GroovyScriptModel model){
        if (model == null){
            return AjaxResult.error("参数不能为空");
        }
        String name = model.getName();
        if (StringUtils.isBlank(name)){
            return AjaxResult.error("脚本名称不能为空");
        }
        String content = model.getContent();
        if (StringUtils.isBlank(content)){
            return AjaxResult.error("脚本内容不能为空");
        }
        /*content = String.valueOf(Base64.decode(content));
        model.setContent(content);*/
        try {
            groovyScriptService.saveOrUpdate(model);
            Map<String, String> data = new HashMap<String, String>();
            data.put("id", model.getId());
            return AjaxResult.success( data);
        } catch (Exception e) {
            log.error("保存脚本信息异常:{}", JSON.toJSONString(model), e);
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 查询
     * @return
     */
    @GetMapping("list")
    public AjaxResult list(){
        List<GroovyScriptModel> groovyScriptModels = groovyScriptService.selectAll();
        if (CollectionUtils.isEmpty(groovyScriptModels)){
            return AjaxResult.success();
        }
        return AjaxResult.success(groovyScriptModels);
    }

    /**
     * 单个查询
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public AjaxResult list(@PathVariable String id){
        GroovyScriptModel groovyScriptModel = groovyScriptService.getById(id);
        if (groovyScriptModel == null){
            return AjaxResult.error("查无此数据");
        }
        return AjaxResult.success(groovyScriptModel);
    }

    @DeleteMapping("remove/{id}")
    public AjaxResult remove(@PathVariable String id){
        GroovyScriptModel groovyScriptModel = groovyScriptService.getById(id);
        if (groovyScriptModel == null){
            return AjaxResult.error("查无此数据");
        }
        groovyScriptModel.setIsDelete(1);
        groovyScriptModel.setUpdateTime(LocalDateTime.now());
        try {
            groovyScriptService.saveOrUpdate(groovyScriptModel);
            GroovyUtil.delScript(groovyScriptModel.getId(), groovyScriptModel.getType());
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("删除脚本信息失败：:{}", JSON.toJSONString(groovyScriptModel), e);
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 发布
     * @param id
     * @return
     */
    @PostMapping("release/{id}")
    public AjaxResult release(@PathVariable String id){
        GroovyScriptModel groovyScriptModel = groovyScriptService.getById(id);
        if (groovyScriptModel == null){
            return AjaxResult.error("查无此数据");
        }
        boolean release = loadScriptSerivce.release(groovyScriptModel);
        return release ? AjaxResult.success("发布成功") : AjaxResult.error("发布失败");
    }
}
